#include <iostream>
#include <typeinfo>
#include <assert.h>
#include <string>
class Any
{
private:
    // 当Any类中没有存储任何数据的时候，无法确认数据的类型，因此需要Holder指针来表示数据内容
    class Holder
    {
    public:
        virtual const std::type_info& type() = 0;
        virtual Holder* clone() = 0;
        virtual  ~Holder() {}
    };

    // 当Any类存储数据时，需要继承Holder的模板类PlaceHolder来作为数据存储的容器
    template<class T>
    class PlaceHolder: public Holder
    {
    public:
        PlaceHolder(const T& val) :_val(val) {}
        virtual const std::type_info& type() {return typeid(T);}
        virtual Holder* clone() {return new PlaceHolder(_val);}
    public:
        T _val;
    };

public:
    Any() :_content(nullptr){}

    template<class T>
    Any(const T& val) :_content(new PlaceHolder<T>(val)){}

    Any(const Any& other) :_content(other._content ? other._content->clone(): nullptr){}
    
    Any& swap(Any& other)
    {
        std::swap(_content, other._content);
        return *this;
    }

    template<class T>
    Any& operator=(const T& val)
    {
        Any(val).swap(*this);
        return *this;
    }

    Any& operator=(const Any& other)
    {
        Any(other).swap(*this);
        return *this;
    }

    template<class T>
    T* GetContent() 
    {
        assert(typeid(T) == _content->type());
        return &((PlaceHolder<T>*)_content)->_val;
    }
    
    ~Any() {delete _content;}

private:
    Holder* _content;
};